package com.noodles.mall.service.impl;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.noodles.mall.entity.*;
import com.noodles.mall.mapper.OrderMapper;
import com.noodles.mall.service.OrderItemService;
import com.noodles.mall.service.OrderService;
import com.noodles.mall.util.Constant;
import com.noodles.mall.vo.PageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

/** 订单 **/
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {
    @Autowired
    OrderItemService orderItemService;

    /** 增加 **/
    @Transactional(rollbackFor = RuntimeException.class)
    public Order insert(User user,List<Cart> cartList){
        Order order = getOrder(user,cartList);
        super.save(order);
        return selectOne(order.getOrderCode());
    }

    /** 删除: 单个或批量, 交易关闭 **/
    public boolean delete(List<Integer> ids){
        super.update(new UpdateWrapper<Order>().lambda()
                .set(Order::getStatus,Constant.ORDER_CLOSE)
                .in(Order::getId,ids)
        );
        return super.removeByIds(ids);
    }

    /** 修改: 根据id修改所有 **/
    public boolean update(Order order){
        return super.updateById(order);
    }

    /** 单个查询: id **/
    public Order selectOne(Integer id){
        Order order=super.getById(id);
        order.setOrderItemList(orderItemService.selectList(order.getId()));
        return order;
    }

    /** 单个查询: order_code **/
    public Order selectOne(String code){
        return super.getOne(new QueryWrapper<Order>().lambda()
                .eq(Order::getOrderCode,code)
        );
    }

    /** 单个查询: 订单数量 **/
    public Integer selectCount(){
        return super.count();
    }

    /** 分页查询: wrapper **/
    public PageVo selectPage(Long current, Long size, LambdaQueryWrapper<Order> wrapper){
        Page<Order> page = super.page(new Page<>(current, size), wrapper
                .orderByDesc(Order::getCreatedTime)
        );
        page.getRecords().forEach(order -> order.setOrderItemList(
                orderItemService.selectList(order.getId())
        )); // 注入订单商品信息
        return PageVo.getPageVo(page);
    }

    /** 分页查询: 无 **/
    public PageVo selectPage(Long current, Long size){
        return selectPage(current,size,new QueryWrapper<Order>().lambda());
    }

    /** 分页查询: userId **/
    public PageVo selectPage(Long current, Long size, Integer userId){
        return selectPage(current,size,new QueryWrapper<Order>().lambda()
                .eq(Order::getUserId,userId)
        );
    }

    /** 分页查询: word **/
    public PageVo selectPage(Long current, Long size, String word){
        return selectPage(current,size,new QueryWrapper<Order>().lambda()
                .like(Order::getOrderCode,word).or()
                .like(Order::getUserId,word).or()
        );
    }

    /** 生成订单 **/
    private Order getOrder(User user,List<Cart> cartList) {
        Order order = new Order();
        order.setUserId(user.getId());
        order.setOrderCode(DatePattern.PURE_DATETIME_MS_FORMAT.format(DateUtil.date()));
        order.setPayment(cartList.stream()
                .map(cart -> cart.getItem().getSalePrice().multiply(new BigDecimal(cart.getItemCount())))
                .reduce(BigDecimal.ZERO, BigDecimal::add));
        order.setStatus(Constant.ORDER_UNPAID);
        order.setReceiverName(user.getNickname());
        order.setReceiverMobile(user.getMobile());
        order.setReceiverAddress(user.getAddress());
        return order;
    }
}
